#!/usr/bin/env python
# -*- coding: utf-8 -*-

def is_over(ponto, image, posicao, botao = False, som = None):
    # RETORNA TRUE SE UM PONTO ESTÁ SOBRE O BOTÃO
    ponto_x, ponto_y = ponto
    w, h = image.get_size()
    x, y = posicao
    in_x = ponto_x >= x and ponto_x < x + w
    in_y = ponto_y >= y and ponto_y < y + h
    if botao and in_x and in_y:
        som.play(0)
    return in_x and in_y
   
def jogar(music):
    
    from os import sep
    import pygame
    from pygame.locals import QUIT, MOUSEBUTTONDOWN, MOUSEMOTION, KEYDOWN, K_ESCAPE
    from sys import exit
    from game import game
    
    pygame.init()
    
    screensize = (900,600)
    screen = pygame.display.set_mode(screensize,0,32)
    
    botao_sound = pygame.mixer.Sound('sounds'+sep+'botao_sound.wav')
    b_menu = pygame.image.load('images'+sep+'botoes'+sep+'b_menu.png').convert_alpha()
    b_menu_press = pygame.image.load('images'+sep+'botoes'+sep+'b_menu_press.png').convert_alpha()
    back_jogador_1 = pygame.image.load('images'+sep+'menus'+sep+'jogador_1.png').convert()
    back_jogador_2 = pygame.image.load('images'+sep+'menus'+sep+'jogador_2.png').convert()
    android = pygame.image.load('images'+sep+'personagens'+sep+'android.png').convert_alpha()
    android_press = pygame.image.load('images'+sep+'personagens'+sep+'android_press.png').convert_alpha()
    electric = pygame.image.load('images'+sep+'personagens'+sep+'eletric.png').convert_alpha()
    electric_press = pygame.image.load('images'+sep+'personagens'+sep+'eletric_press.png').convert_alpha()
    kim = pygame.image.load('images'+sep+'personagens'+sep+'kim.png').convert_alpha()
    kim_press = pygame.image.load('images'+sep+'personagens'+sep+'kim_press.png').convert_alpha()
    lampiao = pygame.image.load('images'+sep+'personagens'+sep+'lampiao.png').convert_alpha()
    lampiao_press = pygame.image.load('images'+sep+'personagens'+sep+'lampiao_press.png').convert_alpha()
    metralha = pygame.image.load('images'+sep+'personagens'+sep+'metralha.png').convert_alpha()
    metralha_press = pygame.image.load('images'+sep+'personagens'+sep+'metralha_press.png').convert_alpha()
    metranca = pygame.image.load('images'+sep+'personagens'+sep+'metranca.png').convert_alpha()
    metranca_press = pygame.image.load('images'+sep+'personagens'+sep+'metranca_press.png').convert_alpha()
    rec = pygame.image.load('images'+sep+'personagens'+sep+'rec.png').convert_alpha()
    rec_press = pygame.image.load('images'+sep+'personagens'+sep+'rec_press.png').convert_alpha()
    revolt = pygame.image.load('images'+sep+'personagens'+sep+'revolt.png').convert_alpha()
    revolt_press = pygame.image.load('images'+sep+'personagens'+sep+'revolt_press.png').convert_alpha()
            
    x = 125
    y = 206
    y_botoes_1 = y
    x_android = x + 167*0
    x_lampiao = x + 167*1
    x_revolt = x + 167*2
    x_electric = x + 167*3
    y_botoes_2 = y + 193
    x_metranca = x + 167*0
    x_metralha = x + 167*1
    x_kim = x + 167*2
    x_rec = x + 167*3
    x_menu = screensize[0]-b_menu.get_width()-10
    y_menu = screensize[1]-b_menu.get_height()-10
    
    bt_android = android
    bt_lampiao = lampiao
    bt_revolt = revolt
    bt_electric = electric
    bt_metranca = metranca
    bt_metralha = metralha
    bt_kim = kim
    bt_rec = rec
    bt_menu = b_menu
    
    clock = pygame.time.Clock()
    
    player_1, player_2 = None, None

    #LOOP DO PLAYER 1
    while True:
               
        for event in pygame.event.get():
            if event.type==QUIT:
                exit()
                
            if event.type == MOUSEBUTTONDOWN and event.button == 1:
                if is_over(event.pos, android, (x_android, y_botoes_1), True, botao_sound):
                    player_1 = 'android'
                elif is_over(event.pos, lampiao, (x_lampiao, y_botoes_1), True, botao_sound):
                    player_1 = 'lampiao'
                elif is_over(event.pos, revolt, (x_revolt, y_botoes_1), True, botao_sound):
                    player_1 = 'revolt'
                elif is_over(event.pos, electric, (x_electric, y_botoes_1), True, botao_sound):
                    player_1 = 'electric'
                elif is_over(event.pos, metranca, (x_metranca, y_botoes_2), True, botao_sound):
                    player_1 = 'metranca'
                elif is_over(event.pos, metralha, (x_metralha, y_botoes_2), True, botao_sound):
                    player_1 = 'metralha'
                elif is_over(event.pos, kim, (x_kim, y_botoes_2), True, botao_sound):
                    player_1 = 'kim'
                elif is_over(event.pos, rec, (x_rec, y_botoes_2), True, botao_sound):
                    player_1 = 'rec'
                elif is_over(event.pos, b_menu, (x_menu, y_menu), True, botao_sound):
                    return
        
            if event.type == MOUSEMOTION:
                    
                if is_over(event.pos, android, (x_android, y_botoes_1)):
                    bt_android = android_press
                else:
                    bt_android = android
                    
                if is_over(event.pos, lampiao, (x_lampiao, y_botoes_1)):
                    bt_lampiao = lampiao_press
                else:
                    bt_lampiao = lampiao
                    
                if is_over(event.pos, revolt, (x_revolt, y_botoes_1)):
                    bt_revolt = revolt_press
                else:
                    bt_revolt = revolt
                
                if is_over(event.pos, electric, (x_electric, y_botoes_1)):
                    bt_electric = electric_press
                else:
                    bt_electric = electric
                    
                if is_over(event.pos, metranca, (x_metranca, y_botoes_2)):
                    bt_metranca = metranca_press
                else:
                    bt_metranca = metranca
                    
                if is_over(event.pos, metralha, (x_metralha, y_botoes_2)):
                    bt_metralha = metralha_press
                else:
                    bt_metralha = metralha
                    
                if is_over(event.pos, kim, (x_kim, y_botoes_2)):
                    bt_kim = kim_press
                else:
                    bt_kim = kim
                    
                if is_over(event.pos, rec, (x_rec, y_botoes_2)):
                    bt_rec = rec_press
                else:
                    bt_rec = rec
                    
                if is_over(event.pos, b_menu, (x_menu, y_menu)):
                    bt_menu = b_menu_press
                else:
                    bt_menu = b_menu
                    
        if event.type == KEYDOWN:
            if event.key == K_ESCAPE:
                return
        
        if player_1 is not None:
            break            
        
        clock.tick(40) 
        
        screen.blit(back_jogador_1,(0,0))
        
        #EXIBIÇÃO DOS PERSONAGENS
        screen.blit(bt_android, (x_android,y_botoes_1))
        screen.blit(bt_lampiao, (x_lampiao,y_botoes_1))
        screen.blit(bt_revolt, (x_revolt,y_botoes_1))
        screen.blit(bt_electric, (x_electric,y_botoes_1))
        screen.blit(bt_metranca, (x_metranca,y_botoes_2))
        screen.blit(bt_metralha, (x_metralha,y_botoes_2))
        screen.blit(bt_kim, (x_kim,y_botoes_2))
        screen.blit(bt_rec, (x_rec,y_botoes_2))
        screen.blit(bt_menu, (x_menu,y_menu))
            
        pygame.display.update()
        
    #LOOP DO PLAYER 2
    while True:
               
        for event in pygame.event.get():
            if event.type==QUIT:
                exit()
                
            if event.type == MOUSEBUTTONDOWN and event.button == 1:
                if is_over(event.pos, android, (x_android, y_botoes_1), True, botao_sound):
                    player_2 = 'android'
                elif is_over(event.pos, lampiao, (x_lampiao, y_botoes_1), True, botao_sound):
                    player_2 = 'lampiao'
                elif is_over(event.pos, revolt, (x_revolt, y_botoes_1), True, botao_sound):
                    player_2 = 'revolt'
                elif is_over(event.pos, electric, (x_electric, y_botoes_1), True, botao_sound):
                    player_2 = 'electric'
                elif is_over(event.pos, metranca, (x_metranca, y_botoes_2), True, botao_sound):
                    player_2 = 'metranca'
                elif is_over(event.pos, metralha, (x_metralha, y_botoes_2), True, botao_sound):
                    player_2 = 'metralha'
                elif is_over(event.pos, kim, (x_kim, y_botoes_2), True, botao_sound):
                    player_2 = 'kim'
                elif is_over(event.pos, rec, (x_rec, y_botoes_2), True, botao_sound):
                    player_2 = 'rec'
                elif is_over(event.pos, b_menu, (x_menu, y_menu), True, botao_sound):
                    return
            
            if event.type == MOUSEMOTION:
                    
                if is_over(event.pos, android, (x_android, y_botoes_1)):
                    bt_android = android_press
                else:
                    bt_android = android
                    
                if is_over(event.pos, lampiao, (x_lampiao, y_botoes_1)):
                    bt_lampiao = lampiao_press
                else:
                    bt_lampiao = lampiao
                    
                if is_over(event.pos, revolt, (x_revolt, y_botoes_1)):
                    bt_revolt = revolt_press
                else:
                    bt_revolt = revolt
                
                if is_over(event.pos, electric, (x_electric, y_botoes_1)):
                    bt_electric = electric_press
                else:
                    bt_electric = electric
                    
                if is_over(event.pos, metranca, (x_metranca, y_botoes_2)):
                    bt_metranca = metranca_press
                else:
                    bt_metranca = metranca
                    
                if is_over(event.pos, metralha, (x_metralha, y_botoes_2)):
                    bt_metralha = metralha_press
                else:
                    bt_metralha = metralha
                    
                if is_over(event.pos, kim, (x_kim, y_botoes_2)):
                    bt_kim = kim_press
                else:
                    bt_kim = kim
                    
                if is_over(event.pos, rec, (x_rec, y_botoes_2)):
                    bt_rec = rec_press
                else:
                    bt_rec = rec
                    
                if is_over(event.pos, b_menu, (x_menu, y_menu)):
                    bt_menu = b_menu_press
                else:
                    bt_menu = b_menu
                    
        if event.type == KEYDOWN:
            if event.key == K_ESCAPE:
                return
        
        if player_2 is not None:
            music.stop()
            game(player_1, player_2)
            return
        
        clock.tick(40) 
        
        screen.blit(back_jogador_2,(0,0))
        
        #EXIBIÇÃO DOS PERSONAGENS
        screen.blit(bt_android, (x_android,y_botoes_1))
        screen.blit(bt_lampiao, (x_lampiao,y_botoes_1))
        screen.blit(bt_revolt, (x_revolt,y_botoes_1))
        screen.blit(bt_electric, (x_electric,y_botoes_1))
        screen.blit(bt_metranca, (x_metranca,y_botoes_2))
        screen.blit(bt_metralha, (x_metralha,y_botoes_2))
        screen.blit(bt_kim, (x_kim,y_botoes_2))
        screen.blit(bt_rec, (x_rec,y_botoes_2))
        screen.blit(bt_menu, (x_menu,y_menu))
            
        pygame.display.update()

def instrucoes():
    
    from os import sep
    import pygame
    from pygame.locals import QUIT, MOUSEBUTTONDOWN, MOUSEMOTION
    from sys import exit
    
    pygame.init()
    
    screensize = (900,600)
    screen = pygame.display.set_mode(screensize,0 ,32)
    botao_sound = pygame.mixer.Sound('sounds'+sep+'botao_sound.wav')
    b_menu = pygame.image.load('images'+sep+'botoes'+sep+'b_menu.png').convert_alpha()
    b_menu_press = pygame.image.load('images'+sep+'botoes'+sep+'b_menu_press.png').convert_alpha()
    back_instrucoes = pygame.image.load('images'+sep+'menus'+sep+'instrucoes.png').convert()
    
    x_menu = screensize[0]-b_menu.get_width()-10
    y_menu = screensize[1]-b_menu.get_height()-10
    bt_menu = b_menu
    
    clock = pygame.time.Clock()
    
    while True:
        
        for event in pygame.event.get():
            if event.type==QUIT:
                exit()
            if event.type == MOUSEBUTTONDOWN and event.button == 1:
                if is_over(event.pos, b_menu, (x_menu, y_menu), True, botao_sound):
                    return
            if event.type == MOUSEMOTION:
                if is_over(event.pos, b_menu, (x_menu, y_menu)):
                    bt_menu = b_menu_press
                else:
                    bt_menu = b_menu
                    
        clock.tick(40)
                                
        screen.blit(back_instrucoes, (0,0))
        screen.blit(bt_menu, (x_menu,y_menu))

        pygame.display.update()
        
def creditos():
    
    from os import sep
    import pygame
    from pygame.locals import QUIT, MOUSEBUTTONDOWN, MOUSEMOTION
    from sys import exit
    
    pygame.init()
    
    screensize = (900,600)
    screen = pygame.display.set_mode(screensize,0 ,32)
    botao_sound = pygame.mixer.Sound('sounds'+sep+'botao_sound.wav')
    back_creditos = pygame.image.load('images'+sep+'menus'+sep+'creditos.png').convert()
    b_menu = pygame.image.load('images'+sep+'botoes'+sep+'b_menu.png').convert_alpha()
    b_menu_press = pygame.image.load('images'+sep+'botoes'+sep+'b_menu_press.png').convert_alpha()
    
    x_menu = screensize[0]-b_menu.get_width()-10
    y_menu = screensize[1]-b_menu.get_height()-10
    bt_menu = b_menu
    
    clock = pygame.time.Clock()
    
    while True:
        
        for event in pygame.event.get():
            if event.type==QUIT:
                exit()
            if event.type == MOUSEBUTTONDOWN and event.button == 1:
                if is_over(event.pos, b_menu, (x_menu, y_menu), True, botao_sound):
                    return
            if event.type == MOUSEMOTION:
                if is_over(event.pos, b_menu, (x_menu, y_menu)):
                    bt_menu = b_menu_press
                else:
                    bt_menu = b_menu
                    
        clock.tick(40)
                                
        screen.blit(back_creditos, (0,0))
        screen.blit(bt_menu, (x_menu,y_menu))

        pygame.display.update()